<?php


namespace app\home\controller\user;

use app\home\controller\Base;
use app\home\model\dealer\Dealer;
use app\home\service\TokenService;
use app\home\service\UserToken;
use app\home\service\CodeService;
use app\lib\api\LoginOrRegisterFormValidate;
use app\home\model\PhoneYzm;
class Login extends Base
{

    protected $model;
    protected $dealerInvite;
    protected $param;
    protected $phoneYzm;

    /**
     * Login constructor.
     * @param Dealer $model
     */
    public function __construct(Dealer $model,PhoneYzm $phoneYzm)
    {
        $this->model = $model;
        $this->phoneYzm = $phoneYzm;
        $this->param = input('post.');
    }
    public static function parkPhone(){
        return ['17381941314'];
    }
    /**
     * 用户登录
     * @param int phone 手机号码
     * @param int code  短信验证码(待接入)
     */
    public function userLogin()
    {
        (new LoginOrRegisterFormValidate())->goCheck();
        $userInfo=$this->getUserData();
        if(!in_array($this->param['phone'],self::parkPhone())){
            if(config('yzm')===true){
                if($this->msgCode() != $this->param['code'])  return CodeService::VERIFICATION_CODE_ERROR;
            }
        }
        if(empty($userInfo)){
            $this->loginRegister();
        };   //未注册
        $token = $this->updateToken($this->createToken(),$this->param['phone']);
        return  $token ? CodeService::Response(200,'登录成功',['token'=>$token]) : CodeService::SERVER_ERROR_TRY_AGAIN_LATER;
    }

    public function pwdLogin()
    {
        $phone = num_secret($this->param['phone']);
        $r = db('dealer')->where('phone',$phone)->where('pwd',$this->param['pwd'])->find();
        if(!$r){
            return ['code'=>400,'msg'=>'密码错误'];
        }
        $token = $this->updateToken($this->createToken(),$this->param['phone']);
        return  $token ? CodeService::Response(200,'登录成功',['token'=>$token]) : CodeService::SERVER_ERROR_TRY_AGAIN_LATER;
    }

    public function reset()
    {
        $token = $this->param['token'];
        $r = db('dealer')->where('token',$token)->where('pwd',$this->param['pwd'])->find();
        if(!$r){
            return ['code'=>400,'msg'=>'密码错误'];
        }
        $this->model->save(['pwd'=>$this->param['newpwd']],['token'=>$token]);
        return CodeService::Response(200,'修改成功');
    }

    public function setpwd()
    {
        $token = $this->param['token'];
        $this->model->save(['pwd'=>$this->param['newpwd']],['token'=>$token]);
        return CodeService::Response(200,'修改成功');
    }

    public function forget()
    {
        $this->model->save(['pwd'=>$this->param['pwd']],['phone'=>num_secret($this->param['phone'])]);
        return CodeService::Response(200,'修改成功');
    }

    public function du()
    {
        $arr = [
            'system'=>[
                [
                    'title'=>'您查询的报告已经获取成功',
                    'content' =>'您于2019-08-12 15：28查询的信用报...'
                ],
                [
                    'title'=>'您查询的报告已经获取成功',
                    'content' =>'您于2019-08-12 15：28查询的信用报...'
                ],
                [
                    'title'=>'您查询的报告已经获取成功',
                    'content' =>'您于2019-08-12 15：28查询的信用报...'
                ],
            ],
            'replay' =>[
                [
                    'title' =>'大话西游',
                    'content'=>'真的有这么好吗？',
                    'time'=>'8-29 15:40'
                ], [
                    'title' =>'大话西游',
                    'content'=>'真的有这么好吗？',
                    'time'=>'8-29 15:40'
                ], [
                    'title' =>'大话西游',
                    'content'=>'真的有这么好吗？',
                    'time'=>'8-29 15:40'
                ], [
                    'title' =>'大话西游',
                    'content'=>'真的有这么好吗？',
                    'time'=>'8-29 15:40'
                ], [
                    'title' =>'大话西游',
                    'content'=>'真的有这么好吗？',
                    'time'=>'8-29 15:40'
                ], [
                    'title' =>'大话西游',
                    'content'=>'真的有这么好吗？',
                    'time'=>'8-29 15:40'
                ],
            ]
        ];
        return ['code'=>200,'msg'=>'获取成功','data'=>$arr];
    }

    public function resetImg()
    {
        $token = $this->param['token'];

        $this->model->save(['headimg'=>$this->param['headimg']],['token'=>$token]);
        return CodeService::Response(200,'修改成功');
    }

    public function nk()
    {
        $token = $this->param['token'];

        $this->model->save(['nick_name'=>$this->param['nick_name']],['token'=>$token]);
        return CodeService::Response(200,'修改成功');
    }


    private function msgCode(){
        $code = $this->phoneYzm->where('phone', num_secret($this->param['phone']))->value('yzm');
        return $code;
    }

    private function createToken()
    {
        $token =UserToken::createToken();
        cookie(md5(config('salt')), $token);
        return $token;
    }

    private function updateToken($token,$phone)
    {
        $this->model->save(['token'=>$token],['phone'=>num_secret($phone)]);
        return $token;
    }

    private function loginRegister()
    {
        $map = [
            'phone' => num_secret($this->param['phone']),
            'nick_name'  =>tel_deal($this->param['phone']),
            'dealer_code' =>randomString(8),
            'create_time' =>date_time()
        ];
        $Dealer=new Dealer();
        return  $Dealer->insert($map);
    }
    public function getUserData(){
        $result = $this->model->where('phone', num_secret($this->param['phone']))->field(['u_id','phone'])->find();
        return $result;
    }

}
